package com.shanjupay.common.utilbak.apicommon;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;


/**
 * 接口请求Request处理工具类
 *
 * @author p00130004
 * @version [版本号, 2019-04-16]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class RequestTools {


    /**
     * 获取iosdeviceNo
     *
     * @param request
     * @return
     * @Autho
     * @Date 2017-4-24 - 下午2:01:43
     */
    public static String getIosDeviceId(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_X_DEVICE_UUID.getHead());
    }

    /**
     * 获取devicebrand
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getDeviceBrand(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_DEVICE_BRAND.getHead());
    }

    /**
     * 获取deviceName
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getDeviceName(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_DEVICE_NAME.getHead());
    }

    /**
     * 获取osVersion
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getOsVersion(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_OS_VERSION.getHead());
    }

    /**
     * 获取operator
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getOperator(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_OPERATOR.getHead());
    }

    /**
     * 获取deviceid
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getDeviceId(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_DEVICE_ID.getHead());
    }

    /**
     * 获取deviceToken
     *
     * @param request
     * @return
     */
    public static String getDeviceToken(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_DEVICE_TOKEN.getHead());
    }


    /**
     * 获取deviceid
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getSchoolId(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_SCHOOL_ID.getHead());
    }


    /**
     * 方法名称: getClassId
     * 方法描述:班级ID
     * 创建人：黄邵平
     *
     * @param request
     * @return
     */
    public static String getClassId(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_CLASS_ID.getHead());
    }

    /**
     * 获取移动终端类型
     * <p>
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getMobileType(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_MOBILE_TYPE.getHead());
    }

    /**
     * 获取客户端版本
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getVersion(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_VERSION.getHead());
    }

    /**
     * 获取客户端mac
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getMac(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_MAC.getHead());
    }

    /**
     * 获取客服端显示器分辨率
     * <功能详细描述>
     *
     * @param request
     * @return String [返回类型说明]
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getResolution(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_RESOLUTION.getHead());
    }

    /***
     * 获取 cache-control控制方式
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getCacheControl(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_CACHE_CONTROL.getHead());
    }

    /**
     * 获取请求中要求的响应体编码方式
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getEncodingType(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_ENCODING_TYPE.getHead());
    }

    /**
     * 获取用户密码
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getUserHash(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_USER_HASH.getHead());
    }

    /***
     * 获取请求路径
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getAction(HttpServletRequest request) {
        String[] path = request.getRequestURI().split("/");
        return StringUtils.trim(path[path.length - 1]);
    }


    /**
     * 获取请求中的User-Agent设备类型
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getUserAgent(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");

        if (null != userAgent && !"".equals(userAgent)) {
            userAgent = userAgent.replaceAll("\\|", "/");
        }

        return userAgent;
    }

    /**
     * 获取请求的IP地址
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getRequestIP(HttpServletRequest request) {
        return request.getRemoteAddr();
    }

    /**
     * 获取用户userId
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getUserId(HttpServletRequest request) {
        String userId = request.getHeader(HttpHeadEnum.HTTP_HEAD_USER_ID.getHead());
        return userId;
    }

    /**
     * 获取用户账号id
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getAccountId(HttpServletRequest request) {
        String userId = request.getHeader(HttpHeadEnum.HTTP_HEAD_ACCOUNT_ID.getHead());
        return userId;
    }


    /**
     * 获取时间字符串
     *
     * @param format 时间的格式
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getCurrentTime(String format) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format, new Locale("EN"));
        return sdf.format(date);
    }


    /**
     * 获取客户端请求中接受的语言
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String getAcceptLanguage(HttpServletRequest request) {
        return request.getHeader(HttpHeadEnum.HTTP_HEAD_ACCEPT_LANGUAGE.getHead());
    }

    /**
     * 获取客户端接受的语言
     *
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static Locale getAcceptLocale(HttpServletRequest request) {
        Locale locale = Locale.getDefault();
        String acceptLanguage = getAcceptLanguage(request);
        if (!StringUtils.isEmpty(acceptLanguage)) {
            String[] langs = acceptLanguage.split("_");
            if (langs.length == 1) {
                locale = new Locale(langs[0]);
            } else if (langs.length == 2) {
                locale = new Locale(langs[0], langs[1]);
            } else if (langs.length == 3) {
                locale = new Locale(langs[0], langs[1], langs[2]);
            }
        }

        return locale;
    }

    /**
     * 设置响应消息中的时间戳
     *
     * @param response
     * @see [类、类#方法、类#成员]
     */
    public static void setTimeStamp(HttpServletResponse response, String timeStamp) {
        response.setHeader(HttpHeadEnum.HTTP_HEAD_TIMESTAMP.getHead(), timeStamp);
    }

    /**
     * 初始化所有head集合
     *
     * @param request
     */
    private Map<String, Object> initHead(HttpServletRequest request) {

        Map<String, Object> resutMap = new HashMap<>();
        for (HttpHeadEnum headEnum : HttpHeadEnum.values()) {
            resutMap.put(headEnum.getHead(), request.getHeader(headEnum.getHead()));
        }
        return resutMap;
    }


}
